package com.ibiaoke.boss.controller;

import com.ibiaoke.common.base.BaseController;
import com.ibiaoke.common.base.Page;
import com.ibiaoke.common.response.Res;
import com.ibiaoke.common.utils.AssertUtils;
import com.ibiaoke.customer.entity.Customer;
import com.ibiaoke.order.entity.ShoppingCart;
import com.ibiaoke.order.entity.ShoppingCartGoods;
import com.ibiaoke.order.service.ShoppingCartGoodsService;
import com.ibiaoke.order.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 购物车
 * Created by Biaoke on 2017/6/26.
 */
@Controller
public class WxShoppingCartController extends BaseController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @Autowired
    private ShoppingCartGoodsService shoppingCartGoodsService;

    /**
     * 加入购物车
     *
     * @param goodsId
     * @param num
     * @param shopId
     * @return
     */
    @ResponseBody
    @RequestMapping("/saveShoppingCart")
    public Res saveShoppingCart(Integer goodsId, Integer num, Integer shopId) {
        //用户是否登录
        Customer customer = getLoginUser();
        AssertUtils.notNull(customer, "请先登录");
        shoppingCartService.saveShoppingService(customer.getCustomerId(), shopId, goodsId, num);
        return Res.create();
    }

    /**
     * 购物车列表
     *
     * @param shopId
     * @return
     */
    @ResponseBody
    @RequestMapping("/listShoppingCart")
    public Res listShoppingCart(Integer shopId) {
        /*Customer customer = getLoginUser();
        AssertUtils.notNull(customer, "请先登录");*/
        Customer customer = new Customer();
        customer.setCustomerId(525356);
        List<ShoppingCart> shoppingCartList = shoppingCartService.listShoppingCart(customer.getCustomerId(), shopId);
        return Res.create().addResponse("shoppingCartList", shoppingCartList);
    }

    /**
     * 删除购物车
     *
     * @param cartId
     * @return
     */
    @ResponseBody
    @RequestMapping("/batchDeleteShoppingCart")
    public Res batchDeleteShoppingCart(String cartId) {
        Customer customer = getLoginUser();
        AssertUtils.notNull(customer, "请先登录");

        shoppingCartService.batchDeleteShoppingCart(cartId, customer.getCustomerId());
        return Res.create();
    }


    /**
     * 购物车数量
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/getShoppingCartNum")
    public Res getShoppingCartNum() {
        Customer customer = getLoginUserSafely();
        long count = 0L;
        if (customer != null) {
            count = shoppingCartService.getShoppingCartNum(customer.getCustomerId());
        }
        return Res.create().addResponse("count", count);
    }
    /****************************************************新版微商城_____购物车**************************************************************/

    /**
     * 加入购物车
     * @param goodsId
     * @param num
     * @param shopId
     * @return
     */
    @ResponseBody
    @RequestMapping("/newSaveShoppingCart")
    public Res newSaveShoppingCart(Integer goodsId, Integer num, Integer shopId) {
        Customer customer = getLoginUser();
        AssertUtils.notNull(customer, "请先登录");
        shoppingCartService.newSaveShoppingCart(customer.getCustomerId(), shopId, goodsId, num);
        return Res.create();
    }
    /**
     * 删除购物车
     * @param cartId
     * @return
     */
    @ResponseBody
    @RequestMapping("/newDeleteShopppingCart")
    public Res newDeleteShopppingCart(String cartId) {
        Customer customer = getLoginUser();
        AssertUtils.notNull(customer, "请先登录");
        shoppingCartService.newDeleteShopppingCart(customer.getCustomerId(),cartId);
        return Res.create();
    }
    /**
     * 购物车列表
     * @return
     */
    @ResponseBody
    @RequestMapping("/newListShoppingCart")
    public Res newListShoppingCart(Page page) {
        Customer customer = getLoginUser();
        AssertUtils.notNull(customer, "请先登录");
        List<ShoppingCart> shoppingCartList = shoppingCartService.newListShoppingCart(customer.getCustomerId(),page);
        return Res.create().addResponse("shoppingCartList", shoppingCartList);
    }

    /**
     * 修改购物车数量
     * @param shoppingCartGoods
     * @return
     */
    @ResponseBody
    @RequestMapping("/updateCartGoodsNum")
    public Res updateCartGoodsNum(ShoppingCartGoods shoppingCartGoods){
        Customer customer = getLoginUser();
        AssertUtils.notNull(customer, "请先登录");
        shoppingCartService.updateCartGoodsNum(shoppingCartGoods);
        return Res.create();
    }

    /**
     * 购物车数量
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/getShoppingCartNumber")
    public Res getShoppingCartNumber() {
        Customer customer = getLoginUserSafely();
        long count = 0L;
        if (customer != null) {
            count = shoppingCartService.getShoppingCartNumber(customer.getCustomerId());
        }
        return Res.create().addResponse("count", count);
    }
}
